const CONFIG = require('./config.js')
const API_BASE_URL = 'https://wx.ndmooc.com/api/v2/'
const request = (url, needSubDomain, method, data) => {
  let _url = API_BASE_URL + (needSubDomain ? '/' + CONFIG.subDomain : '') + url
  return new Promise((resolve, reject) => {
    wx.request({
      url: _url,
      method: method,
      data: data,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(request) {
        resolve(request.data)
      },
      fail(error) {
        reject(error)
      },
      complete(aaa) {
        // 加载完成
      }
    })
  })
}

const requestOther = (url, needSubDomain, method, data) => {
  let _url = API_BASE_URL + (needSubDomain ? '/' + CONFIG.subDomain : '') + url
  return new Promise((resolve, reject) => {
    wx.request({
      url: _url,
      method: method,
      data: data,
      header: {
        'Content-Type': 'application/json'
      },
      success(request) {
        resolve(request.data)
      },
      fail(error) {
        reject(error)
      },
      complete(aaa) {
        // 加载完成
      }
    })
  })
}

/**
 * 小程序的promise没有finally方法，自己扩展下
 */
Promise.prototype.finally = function (callback) {
  var Promise = this.constructor;
  return this.then(
    function (value) {
      Promise.resolve(callback()).then(
        function () {
          return value;
        }
      );
    },
    function (reason) {
      Promise.resolve(callback()).then(
        function () {
          throw reason;
        }
      );
    }
  );
}

module.exports = {
  request,
  loginVerify: (data) => {
    return request('auth/login', false, 'post', data)
  },
  vertifyToken: (data) => {
    return request('auth/token', false, 'post', data)
  },
  loginWx: (data) => {
    return request('login/wx', false, 'post', data)
  },
  batchNew: (data) => {
    return request('users/batch/new', false, 'post', data)
  },
  newUser: (data) => {
    return request('users/new', false, 'post', data)
  },
  addNotes: (data) => {
    return request('notes/new', false, 'post', data)
  },
  notesList: (data) => {
    return request('notes', false, 'get', data)
  },
  wxVerify: (data) => {
    return request('auth/login/wx', false, 'post', data)
  },
  achieveOid: (data) => {
    return request('auth/wx', false, 'post', data)
  },
  openSecret: (data) => {
    return request('auth/wx', false, 'post', data)
  },
  loginPhone: (data) => {
    return request('auth/login', false, 'post', data)
  },
  noteNew: (data) => {
    return request('notes/new', false, 'post', data)
  },
  pensSelf: (data) => {
    return request('pens/self', false, 'get', data)
  },
  notesSelf: (data) => {
    return request('notes/self', false, 'get', data)
  },
  newPens: (data) => {
    return request('pens/new', false, 'post', data)
  },
  pens: (data) => {
    return request('pens', false, 'get', data)
  },
  updata: (data) => {
    return request('notes/update', false, 'post', data)
  },
  pageInfo: (data) => {
    return request('notes/page/info', false, 'get', data)
  },
  likes: (data) => {
    return request('notes/likes', false, 'post', data)
  },
  listUpdate: (data) => {
    return request('notes/info/update', false, 'post', data)
  },
  share: (data) => {
    return request('notes/share', false, 'post', data)
  },
  shares: (data) => {
    return request('notes/share/self', false, 'get', data)
  },
  remove: (data) => {
    return request('notes/remove', false, 'DELETE', data)
  },
  phone: (data) => {
    return request('auth/phone/binding', false, 'post', data)
  },
  code: (data) => {
    return request('auth/phone/code', false, 'post', data)
  },
  file: (data) => {
    return request('notes/file', false, 'post', data)
  },
  fileOther: (data) => {
    return requestOther('notes/file', false, 'post', data)
  },
  checkSings: (data) => {
    return request('users/music', false, 'get', data)
  },
  imgUpload: (data) => {
    return request('file/upload', false, 'post', data)
  },
  checkbgs: (data) => {
    return request('users/bgs', false, 'get', data)
  },
  templates: (data) => {
    return request('templates', false, 'get', data)
  },
  pages: (data,token) => {
    return request('templates/' + data._id + '/pages', false, 'get', token)
  },
  templateSome: (data) => {
    return request('notes/list/self', false, 'get', data)
  },
  idTemplate: (data, token) => {
    return request('templates/' + data.id + '/pages/context', false, 'get', token)
  },
  convert: (data) => {
    return requestOther('notes/file/convert', false, 'post', data)
  },
  setting: (data) => {
    return request('setting/self', false, 'get', data)
  },
  settingNew: (data) => {
    return requestOther('setting/self/new', false, 'post', data)
  },
  settingBgs: (data) => {
    return request('setting/bgs', false, 'get', data)
  },
  editSetting: (data) => {
    return requestOther('setting/self/edit', false, 'post', data)
  },
  shareCode: (data) => {
    return request('share/note', false, 'post', data)
  },
  comments: (id, data) => {
    return request('notes/' + id + '/comments', false, 'get', data)
  },
  commentsNew: (id, data) => {
    return request('notes/' + id + '/comments/new', false, 'post', data)
  },
  deleteComments: (id, data) => {
    return request('comments/' + id + '/delete', false, 'delete', data)
  },
  updateComments: (id, data) => {
    return request('comments/' + id + '/update', false, 'post', data)
  },
  shareNotes: (data) => {
    return request('user/notes', false, 'get', data)
  },
  getData: (data) => {
    return request('notes/note', false, 'get', data)
  },
  sendData: (data) => {
    return request('notes/sync/info', false, 'post', data)
  },
  addPags: (data) => {
    return request('dj/notes/add', false, 'post', data)
  },
  uploadImg: (data) => {
    return request('dj/notes/upload', false, 'post', data)
  },
  deletePage: (data) => {
    return request('dj/notes/page/delete', false, 'delete', data)
  },
  deleteNotes: (data) => {
    return request('dj/notes/delete', false, 'delete', data)
  },
  deleteEverys: (data) => {
    return request('notes/list/delete', false, 'delete', data)
  },
  getToken: (data) => {
    return request('wx/access_token', false, 'get', data)
  },
  templatePages: (data) => {
    return request('templates/pages/bgs', false, 'get', data)
  },
  templatePages: (data) => {
    return request('templates/pages/bgs', false, 'get', data)
  },
  pagesBgs: (data) => {
    return request('templates/page/bgs', false, 'get', data)
  },
  textUpload: (data) => {
    return request('generate/tablets/text/picture', false, 'post', data)
  },
  getImg: (data) => {
    return request('generate/image', false, 'post', data)
  },
  getCode: (data) => {
    return request('auth/phone/code', false, 'post', data)
  },
  submitInfo: (data) => {
    return request('auth/phone/binding/update', false, 'post', data)
  },
  listNotes: (data) => {
    return request('notes/list/notes', false, 'get', data)
  },
  listPages: (data) => {
    return request('user/notes', false, 'get', data)
  },
  uploadMp3: (data) => {
    return request('notes/mp3/upload', false, 'get', data)
  },
  sfivePages: (data) => {
    return request('notes/decode/hex', false, 'get', data)
  },
  removePoints: (data) => {
    return request('notes/note/py', false, 'get', data)
  }
}
